class Solution {
public:

    int ladderLength(string startGene, string endGene, vector<string>& bank) {
        unordered_set<string> hash(bank.begin(), bank.end());
        unordered_map<string, bool> sign;
        if (!hash.count(endGene)) return 0;
        if (startGene == endGene) return 1;
        int ret = 1;
        queue<string> q;
        q.push(startGene);
        sign[startGene] = true;
        int number = startGene.size();
        while (!q.empty())
        {
            ret++;
            int size = q.size();
            while (size--)
            {
                string top = q.front();
                q.pop();
                for (int i = 0; i < number; i++)
                {
                    string num = top;
                    for (int k = 0; k < 26; k++)
                    {
                        num[i] = 'a' + k;
                        if (hash.count(num) && !sign[num])
                        {
                            if (num == endGene) return ret;
                            q.push(num);
                            sign[num] = true;
                        }
                    }
                }
            }
        }
        return 0;
    }
};
class Solution {
public:
    int row, col;
    int dx[4] = { 0, 0, 1, -1 };
    int dy[4] = { 1, -1, 0, 0 };
    int cutOffTree(vector<vector<int>>& forest) {
        vector<pair<int, int>> v;
        row = forest.size(), col = forest[0].size();
        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++)
                if (forest[i][j] > 1) v.push_back({ i, j });
        sort(v.begin(), v.end(), [&](const pair<int, int>& p1, const pair<int, int>& p2)
            {
                return forest[p1.first][p1.second] < forest[p2.first][p2.second];
            });
        int bx = 0, by = 0, count = 0;
        for (auto& [x, y] : v)
        {
            int ret = bfs(forest, bx, by, x, y);
            if (ret == -1) return -1;
            count += ret;
            bx = x, by = y;
        }
        return count;
    }
    bool sign[51][51];
    int bfs(vector<vector<int>>& forest, int bx, int by, int ex, int ey)
    {
        if (bx == ex && by == ey) return 0;
        memset(sign, 0, sizeof sign);
        queue<pair<int, int>> q;
        q.push({ bx, by });
        sign[bx][by] = true;
        int ret = 0;
        while (!q.empty())
        {
            int size = q.size();
            ret++;
            while (size--)
            {
                auto [x, y] = q.front();
                q.pop();
                for (int k = 0; k < 4; k++)
                {
                    int i = x + dx[k];
                    int j = y + dy[k];
                    if (i < row && i >= 0 && j >= 0 && j < col && forest[i][j] != 0 && !sign[i][j])
                    {
                        if (i == ex && j == ey) return ret;
                        sign[i][j] = true;
                        q.push({ i, j });
                    }
                }
            }
        }
        return -1;
    }
};






















